package com.yjh.server.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjh.pojo.entity.CourtInfo;
import com.yjh.pojo.entity.ReservationMain;
import com.yjh.pojo.entity.ReserveTimeRelation;
import com.yjh.pojo.entity.User;
import com.yjh.pojo.vo.R;
import com.yjh.pojo.vo.ReserveVo;
import com.yjh.server.service.CourtInfoService;
import com.yjh.server.service.ReservationMainService;
import com.yjh.server.service.ReserveTimeRelationService;
import com.yjh.server.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/reserve")
public class ReservationController {
    @Autowired
    private ReservationMainService reservationMainService;
    @Autowired
    private CourtInfoService courtInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private ReserveTimeRelationService reserveTimeRelationService;

    @GetMapping("/getReservation")
    public R<Page<ReserveVo>> getReservation(@RequestParam(value="current",defaultValue="1") Integer current, @RequestParam(value="pageSize",defaultValue="4") Integer pageSize){
        Page<ReservationMain> reservationMainPage = new Page<>(current,pageSize);

        reservationMainService.page(reservationMainPage);
        Page<ReserveVo> reserveVoPage = new Page<>();
        BeanUtils.copyProperties(reservationMainPage,reserveVoPage,"records");

//        List<ReservationMain> list = reservationMainService.list();

        List<ReserveVo> reserveList = reservationMainPage.getRecords().stream().map(item -> {
//            预约信息
            ReserveVo reserveVo=new ReserveVo();
            BeanUtils.copyProperties(item,reserveVo);
//            球场信息
            CourtInfo courtInfo = courtInfoService.getById(item.getCourtId());
            reserveVo.setCourtName(courtInfo.getCourtName());
//            用户名
            User user = userService.getById(item.getUserId());
            reserveVo.setUserName(user.getUsername());
//            预约时间
            String minMaxTime = reservationMainService.getMinMaxTime(item.getReserveId());
            reserveVo.setReserveTime(minMaxTime);

            return reserveVo;
        }).collect(Collectors.toList());

        reserveVoPage.setRecords(reserveList);

        return R.success(reserveVoPage);
    }

    @DeleteMapping("/del")
    @Transactional
    public R<String> delReservation(@RequestBody List<Integer> ids){

        for(Integer id:ids){
            LambdaQueryWrapper<ReserveTimeRelation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ReserveTimeRelation::getReserveId,id);
//          先删除子表记录
            reserveTimeRelationService.remove(wrapper);

        }

        reservationMainService.removeByIds(ids);
        return R.success("删除成功");
    }
}
